package com.tulele.vtool.utils;

import com.tulele.vtool.model.respEntity.RespData;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author junya
 */
@RestController
public class RestNotFoundFilter implements ErrorController {

    private static final String NOT_FOUND = "404";
    private static final String ERROR_PATH = "/error";

    @RequestMapping(value = ERROR_PATH)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public RespData handleError(HttpServletRequest request) {

        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");

        RespData respData = new RespData();

        if(statusCode == 401){
            respData.setCode(401);
        }else if(statusCode == 404){
            respData.setCode(404);
        }else if(statusCode == 403){
            respData.setCode(403);
        }else{
            respData.setCode(500);
        }

        respData.setMsg("服务不存在");

        return respData;

    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

}
